//
//  SetPassViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/21.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "SetPassViewController.h"
#import "LoginViewController.h"
#import "UIView+Frame.h"
#import "AFNetworking.h"
#import "NSString+GX_MD5.h"
#import "CFMacro.h"
#import "GXNetTool.h"

@interface SetPassViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userPassword;
@property (nonatomic, assign) BOOL isRelogin;

@end

@implementation SetPassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setTitle:@"设置密码"];
}
#pragma mark - 设置新密码
- (IBAction)resetPassword:(id)sender {
    if ([_userPassword.text isEqualToString:@""]) {
        [self addPromptLabelWithTitle:@"密码不能为空"];
        return;
    }
    if (_userPassword.text.length < 6) {
        [self addPromptLabelWithTitle:@"密码为6-16位的数字或字母"];
        return;
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    NSString *passW = [_userPassword.text MD5String32BitsLowercase];
    NSDictionary *sendCode = @{@"contact":_contact,
                               @"check_code":_checkCode,
                               @"new_password":passW};
    [manager.requestSerializer setValue:@"api.kufm.cn" forHTTPHeaderField:@"Host"];
    [manager.requestSerializer setValue:@"d0ce47483e6728610311619cca3fb195" forHTTPHeaderField:@"X-APP-ID"];
    [manager.requestSerializer setValue:@"ios/4.3.0.0.0/appstore/release" forHTTPHeaderField:@"X-API-ID"];
    [manager.requestSerializer setValue:@"0" forHTTPHeaderField:@"X-SESSION-ID"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"96" forHTTPHeaderField:@"Content-Length"];
    // 设置新密码
    [manager POST:@"http://api.kufm.cn/v2/user/reset/passwd"
       parameters:sendCode
         progress:^(NSProgress * _Nonnull uploadProgress) {
         } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             NSLog(@"请求成功:%@", responseObject);
             /**
              * 获取用户
              */
             NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
             /**
              * 修改密码
              */
             [user setObject:_userPassword.text forKey:kPassword];
             /**
              * 重新登录
              */
             [self addPromptLabelWithTitle:@"重置密码成功, 请重新登录"];
             self.isRelogin = YES;
         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             NSLog(@"请求失败:%@", error);
         }];
}
- (void)addPromptLabelWithTitle:(NSString *)title {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -30, self.view.gx_width, 30)];
    label.text = title;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:17];
    label.gx_centerX = self.view.gx_centerX;
    label.backgroundColor = [UIColor grayColor];
    [self.view addSubview:label];
    // 出现
    [UIView animateWithDuration:0.5 animations:^{
        label.gx_y = 0;
        // 显示2秒
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionCurveLinear animations:^{
            label.gx_y = -30;
        } completion:^(BOOL finished) {
            [label removeFromSuperview];
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            loginVC.reloginUser = _contact;
            loginVC.isRelogined = YES;
            [self.navigationController pushViewController:loginVC animated:YES];
        }];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
